package repository

import (
	"boy-go/modules/demo/model"
	"boy-go/modules/demo/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
	"time"
)

var (
	// 查询字段SQL
	_selectTCompanyVo = "id, name, money, type, status, remark, icon, file, update_time, create_time"
)

type TCompanyRepository struct {
	DB *gorm.DB
}

func NewTCompanyRepository() *TCompanyRepository {
	return &TCompanyRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *TCompanyRepository) GetById(id int64) (*model.TCompany, error) {
	db := r.DB
	m := &model.TCompany{}
	err := db.Model(&model.TCompany{}).Where("id = ?", id).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *TCompanyRepository) List(query *vo.QueryTCompanyReq, pageReq *request.PageRequest) ([]*model.TCompany, int64, error) {
	tx := r.DB.Model(&model.TCompany{})
	if query != nil {
		if query.Name != nil {
			tx = tx.Where("name like ?", "%"+*query.Name+"%")
		}
		if query.Money != nil {
			tx = tx.Where("money = ?", *query.Money)
		}
		if query.Type != nil {
			tx = tx.Where("type = ?", *query.Type)
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.Remark != nil {
			tx = tx.Where("remark = ?", *query.Remark)
		}
		if query.Icon != nil {
			tx = tx.Where("icon = ?", *query.Icon)
		}
		if query.File != nil {
			tx = tx.Where("file = ?", *query.File)
		}
		if query.UpdateTime != nil {
			tx = tx.Where("update_time = ?", *query.UpdateTime)
		}
		if query.CreateTime != nil {
			tx = tx.Where("create_time = ?", *query.CreateTime)
		}
		if query.Params != nil {
			beginV, ok1 := query.Params["beginCreateTime"]
			endV, ok2 := query.Params["endCreateTime"]
			if ok1 && ok2 {
				tx = tx.Where("create_time between ? and ?", beginV, endV)
			}
		}
	}
	list := make([]*model.TCompany, 0)
	if pageReq != nil {
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			tx = tx.Select(_selectTCompanyVo)
			tx = db.BuildPageAndOrder(tx, &query.PageRequest, "")
			if err := tx.Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		tx = tx.Select(_selectTCompanyVo)
		if err := tx.Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *TCompanyRepository) Save(m *model.TCompany) error {
	if m.CreateAt == nil {
		currentDate := time.Now()
		m.CreateAt = &currentDate
	}
	return r.DB.Model(&model.TCompany{}).Create(m).Error
}

// Update 更新
func (r *TCompanyRepository) Update(m *model.TCompany) error {
	currentDate := time.Now()
	m.UpdateAt = &currentDate
	return r.DB.Model(&model.TCompany{}).Where("id = ?", m.Id).Omit("id").Updates(m).Error
}

// Delete 删除
func (r *TCompanyRepository) Delete(id int64) error {
	return r.DB.Model(&model.TCompany{}).Where("id = ?", id).Delete(&model.TCompany{}).Error
}

// DeleteBatch 批量删除
func (r *TCompanyRepository) BatchDelete(ids []string) error {
	db := r.DB
	db = db.Where("(id) IN ?", ids)
	return db.Delete(&model.TCompany{}).Error
}
